﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Last03.Aqiantai
{
    /// <summary>
    /// ydsuccess.xaml 的交互逻辑
    /// </summary>
    public partial class ydsuccess : Page
    {
        public ydsuccess()
        {
            InitializeComponent();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            //1 获取时间源
            Button btn = sender as Button;
            using(var context=new HotelEntities())
            {   //实现增删改查
                switch (btn.Content.ToString())
                {
                    case "查询预定"://查，查询
                        ShowResult(context);
                        break;
                    case "删除预定"://删，删除一行
                        string sql = "delete from Yuding where 身份证号={0}";
                        int i=context.Database.ExecuteSqlCommand(sql, txtsfzh.Text);
                        if (i < 0)
                        {
                            MessageBox.Show("请重新输入正确格式的身份证号！");
                        }
                        ShowResult(context);
                        break;
                    //case "更改预定"://改，更改一行
                    //    string ggsql = "update Yuding set 入住时间={0} where 手机号={1}";
                    //    int j = context.Database.ExecuteSqlCommand(ggsql,txtrzrq.Text,txttel.Text);
                    //    if (j<0)
                    //    {
                    //        MessageBox.Show("请重新输入相关信息！");
                    //    }
                    //    ShowResult(context);
                    //    break;
                    default://“增加预定”，增加一行内容
                        string zjsql = "insert into Yuding(预定日期,入住时间,身份证号,手机号,客人姓名) values({0},{1},{2},{3},{4})";
                        int m = context.Database.ExecuteSqlCommand(zjsql, txtydrq.Text, txtrzrq.Text, txtsfzh.Text, txttel.Text, txtname.Text);
                        if (m < 0)
                        {
                            MessageBox.Show("请重新输入正确信息！");
                        }
                        ShowResult(context);
                        break;
                }
            }
        }

        private void ShowResult(HotelEntities context)
        {
            var q = context.Database.SqlQuery<Yuding>("select * from Yuding");
            this.dataGrid.ItemsSource = q.ToList();
        }

        private void txtdel_GotKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e)
        {
            this.txtsfzh.Text = "";
        }

        private void txtrzrq_GotKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e)
        {
            this.txtrzrq.Text = "";
        }

        private void txttel_GotKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e)
        {
            this.txttel.Text = "";
        }

        private void txtydrq_GotKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e)
        {
            this.txtydrq.Text = "";
        }

        private void txtname_GotKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e)
        {
            this.txtname.Text = "";
        }
    }
}
